<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * The project “dev”
 * File name “Menu_model.php”
 *
 * User: znx
 * Company: 思议创想
 * Q  Q: 865303386
 * Site: www.siyi360.com
 *
 * Date: 2017-02-09
 * Time: 15:25
 */
class User_model extends CI_Model {
    private $encryptPassword;

    public function __construct()
    {
        parent::__construct();

        $this->encryptPassword = new PasswordHash(PHPASS_HASH_STRENGTH, PHPASS_HASH_PORTABLE);
    }

    /**
     * get_all_user
     * 获取所有管理后台系统用户数据
     *
     * @return mixed
     */
    public function get_all_user()
    {
        return $this->db
            ->join('admin_role', 'admin_user.user_role = admin_role.role_id', 'left')
            ->order_by('admin_user.user_id ASC')
            ->get('admin_user')
            ->result_array();
    }

    /**
     * get_single_user
     * 获取指定id的单条管理后台系统用户数据
     *
     * @param int $user_id 用户id
     * @return mixed
     */
    public function get_single_user($user_id)
    {
        $table = $this->db->dbprefix('admin_user');
        return $this->db
            ->query('select * from ' . $table . ' where user_id=' . $user_id)
            ->row_array();
    }

    public function repeat_user()
    {
        // 获取表单数据
        $form = $this->input->post();

        $req = $this->db
            ->from('admin_user')
            ->where('user_name', $form['param'])
            ->count_all_results();

        if ($req) {
            return array('info'=>'用户名已经存在', 'status'=>'n');
        } else {
            return array('info'=>'验证通过！', 'status'=>'y');
        }
    }

    /**
     * role_save
     * 保存管理后台用户数据到数据库
     *
     * @return array
     */
    public function user_save()
    {
        // 获取表单数据
        $form = $this->input->post();

        $data = array(
            'user_role' => $form['user_role'],
            'user_realname' => $form['user_realname'],
            'user_email' => $form['user_email'],
            'user_mobile' => $form['user_mobile'],
            'user_status' => $form['user_status']
        );

        // 判断主键ID是否存在，存在则表示更新数据
        if (!array_key_exists('user_id', $form)) {
            // 新增数据
            $data['user_name'] = $form['user_name'];
            $data['user_pass'] = $this->encryptPassword->HashPassword($form['user_pass']);

            $req = $this->db->insert('admin_user', $data);
        } else {
            // 更新数据
            // 有密码表单数据时才提交密码字段
            if (!empty($form['user_pass'])) $data['user_pass'] = $this->encryptPassword->HashPassword($form['user_pass']);

            $req = $this->db->where('user_id', $form['user_id'])->update('admin_user', $data);
        }

        if ($req) {
            return array('status'=>1, 'msg'=>'操作成功！', 'result'=>array('jumpUrl'=>site_url('system//User/index')));
        } else {
            return array('status'=>0, 'msg'=>'操作失败！', 'result'=>'');
        }
    }


    /*
     * user_delete
     * 删除系统用户
     *
     * @access public
     * @param string $user_id 系统用户所属角色id与系统用户id的拼接字符串，格式roleId_Id
     * */
    /**
     * user_delete
     * 删除管理后台系统用户数据
     * @return array
     */
    public function user_delete()
    {
        $user_id = $this->input->get('user_id');

        $req = $this->db->where('user_id', $user_id)->delete('admin_user');
        if ($req) {
            return array('status'=>1, 'msg'=>'操作成功！', 'result'=>'');
        } else {
            return array('status'=>0, 'msg'=>'操作失败！', 'result'=>'');
        }
    }
}